home *** CD-ROM | disk | FTP | other *** search
/ Ham Radio 2000 / Ham Radio 2000.iso / ham2000 / satellit / vstsrc / registry.c < prev    next >
C/C++ Source or Header  |  1995-02-02  |  14KB  |  445 lines

  1. /*
  2.  * %W% %E% %U%  [EXTREL_1.2]
  3.  *
  4.  * VersaTrack Copyright (c) 1993, 1994 Siamack Navabpour. All Rights Reserved.
  5.  *
  6.  * Permission is hereby granted to copy, modify and distribute VersaTrack
  7.  * in whole, or in part, for educational, non-profit and non-commercial use
  8.  * only, free of charge or obligation, and without agreement, provided that
  9.  * all copyrights and restrictions noted herein are observed and followed, and
  10.  * additionally, that this and all other copyright notices listed herein
  11.  * appear unaltered in all copies and in all derived work.
  12.  *
  13.  * This notice shall not in any way void or supersede any of the other authors
  14.  * rights or privileges.
  15.  *
  16.  * VersaTrack IS PRESENTED FREE AND "AS IS", WITHOUT ANY WARRANTY OR SUPPORT.
  17.  * YOU USE IT AT YOUR OWN RISK. The author(s) shall not be liable for any
  18.  * direct, indirect, incidental, or consequential damage, loss of profits or
  19.  * other tangible or intangible losses or benefits, arising out of or related
  20.  * to its use. VersaTrack carries no warranty, explicit or implied, including
  21.  * but not limited to those of merchantablity and fitness for a particular
  22.  * purpose.
  23.  *
  24.  * Siamack Navabpour, 12342 Hunter's Chase Dr. Apt. 2114, Austin, TX 78729.
  25.  * sia@bga.com or sia@realtime.com.
  26.  */
  27.  
  28.  
  29. #include <windows.h>
  30. #include <stdio.h>
  31.  
  32. #include "vstdefs.h"
  33. #include "vsttype.h"
  34. #include "libxtrns.h"
  35. #include "resource.h"       /* Make sure the one in this directory is used */
  36.  
  37. static char *bufp;
  38. static char vkey[100];
  39.  
  40. static void
  41. regfatal(hwnd)
  42. HWND hwnd;
  43. {
  44.     fatal(hwnd, "Unable to create or write entry in system registry");
  45.     Sleep(1000);
  46. }
  47.  
  48.  
  49.  
  50. static LRESULT CALLBACK
  51. InstallProc(hwnd, message, wParam, lParam)
  52. HWND hwnd;
  53. UINT message;
  54. WPARAM wParam;
  55. LPARAM lParam;
  56. {
  57.     POINT *p;
  58.     
  59.     switch(message) {
  60.     case WM_INITDIALOG:
  61.         p = DialogPos(hwnd);
  62.         SendMessage(hwnd, DM_SETDEFID, (WPARAM)IDOK, (LPARAM)0);
  63.         SendDlgItemMessage(hwnd, IDC_INSTALL_MESSAGE, WM_SETTEXT, (WPARAM) 0,
  64.             (LPARAM) bufp);
  65.         SendDlgItemMessage(hwnd, IDC_INSTALL_EDIT, WM_SETTEXT, (WPARAM) 0,
  66.             (LPARAM) tmpbuf);
  67.         SendDlgItemMessage(hwnd, IDC_INSTALL_EDIT, EM_SETSEL, (WPARAM) -1,
  68.             (LPARAM) strlen(tmpbuf)-1);
  69.         SetWindowPos(hwnd, 0, (int)p->x, (int)p->y, 0, 0,
  70.             SWP_NOZORDER | SWP_NOSIZE | SWP_SHOWWINDOW);
  71.         return TRUE;
  72.  
  73.     case WM_COMMAND:
  74.         if (LOWORD(wParam) == IDOK) {
  75.             GetEditText(hwnd, IDC_INSTALL_EDIT, tmpbuf, sizeof(tmpbuf)-1);
  76.             EnableWindow(GetParent(hwnd), TRUE);
  77.             EndDialog(hwnd, TRUE);
  78.             return TRUE;
  79.         }
  80.         if (LOWORD(wParam) == IDCANCEL) {
  81.             EnableWindow(GetParent(hwnd), TRUE);
  82.             EndDialog(hwnd, FALSE);
  83.             return TRUE;
  84.         }
  85.         break;        
  86.  
  87.     case WM_CTLCOLORDLG:
  88.     case WM_CTLCOLORSTATIC:
  89.         SetBkColor((HDC)wParam, BColors[6]);
  90.         SetTextColor((HDC) wParam, BColors[15]);
  91.         return (BOOL) hBDrawBrush[6];
  92.         
  93.     case WM_CTLCOLOREDIT:
  94.         SetBkColor((HDC) wParam, BColors[8]);
  95.         SetTextColor((HDC) wParam, BColors[0]);
  96.         return (BOOL) hBDrawBrush[8];
  97.   
  98.     default:
  99.         break;
  100.     }
  101.     return FALSE;
  102. }
  103.  
  104. BOOL
  105. GetUserInput(hwnd, text, defstr, resultptr, rsize)
  106. HWND hwnd;
  107. char *text, *defstr, *resultptr;
  108. int rsize;
  109. {
  110.     BOOL r;
  111.     extern LRESULT CALLBACK InstallProc();
  112.  
  113.     bufp = text ? text : "";
  114.     strncpy(tmpbuf, defstr ? defstr : "" , sizeof(tmpbuf));
  115.  
  116.     r = (BOOL) DialogBox(hInstDLL, MAKEINTRESOURCE(IDD_INSTALL), hwnd, InstallProc);
  117.  
  118.     if (r && resultptr)
  119.         strncpy(resultptr, tmpbuf, rsize);
  120.  
  121.     return r;
  122. }
  123.  
  124. BOOL
  125. Install(int version, HWND hwnd, HWND hkey, registry_t *setp,
  126.         reginfo_t *rgp, int nstp, BOOL installflag)
  127. {
  128.     int result, i;
  129.     BOOL r;
  130.     registry_t *stp;
  131.     extern void RegCleanup(int, HKEY, reginfo_t *);
  132.  
  133.     if (hkey)
  134.         RegCleanup(version, hkey, rgp);
  135.  
  136.      if (!installflag) do {
  137.         for (i=0, r = TRUE, stp = setp; r && i< nstp; i++, stp++) {
  138.             if (stp->st_keytype != REG_SZ)
  139.                 continue;
  140.             r = TRUE;
  141.             if (stp->st_message)
  142.                 r = GetUserInput(hwnd, stp->st_message, stp->st_default,
  143.                                 stp->st_keyvalue, stp->st_maxkeylen);
  144.             if (r)
  145.                 stp->st_keylen = strlen(stp->st_keyvalue);
  146.         }
  147.         if (!r)
  148.             return FALSE;
  149.     } while(!yesno(hwnd, "Is the information you have supplied correct ?"));
  150.  
  151.     sprintf(vkey,"Software\\%s%s%s%s", rgp->ri_basekey, rgp->ri_subkey1,
  152.         rgp->ri_subkey2, rgp->ri_subkey3);
  153.  
  154.     result = 0;
  155.  
  156.     if (RegCreateKeyEx(HKEY_CURRENT_USER, vkey,
  157.             0, 0, REG_OPTION_NON_VOLATILE, KEY_CREATE_SUB_KEY|KEY_SET_VALUE,
  158.             NULL, &hkey, &result) != ERROR_SUCCESS)
  159.         regfatal(hwnd);
  160.  
  161.     for (i=0, stp = setp; i < nstp ; i++, stp++) {
  162.         if (RegSetValueEx(hkey, stp->st_keyname, 0, stp->st_keytype,
  163.             stp->st_keyvalue, stp->st_keylen ) != ERROR_SUCCESS)
  164.             regfatal(hwnd);
  165.         RegFlushKey(hkey);
  166.     }
  167.  
  168.     RegCloseKey(hkey);
  169.     return TRUE;
  170. }
  171.  
  172. #ifdef WINNT_31
  173.  
  174. void
  175. RegCleanup(version, hxkey, rgp)
  176. int version;
  177. HKEY hxkey;
  178. reginfo_t *rgp;
  179. {
  180.     int  i, result;
  181.     char *cp, vkey[200];
  182.     HKEY *keyp, tmpkey = NULL;
  183.  
  184.     keyp = (hxkey) ? &hxkey : &tmpkey;
  185.     if (hxkey)
  186.         RegCloseKey(hxkey);
  187.  
  188.     sprintf(vkey,"\\Software\\");
  189.  
  190.     for(i=10, cp = rgp->ri_basekey; cp && *cp; cp++)
  191.         if (*cp != '\\')
  192.            vkey[i++] = *cp;
  193.     vkey[i] = 0;
  194.  
  195.     if (RegCreateKeyEx(HKEY_CURRENT_USER, vkey, 0, 0,
  196.         REG_OPTION_NON_VOLATILE, KEY_CREATE_SUB_KEY, NULL, keyp, &result) != ERROR_SUCCESS)
  197.         return;
  198.  
  199.     for(i=0, cp = rgp->ri_subkey1; cp && *cp; cp++)
  200.         if (*cp != '\\')
  201.            vkey[i++] = *cp;
  202.     vkey[i] = 0;
  203.  
  204.     RegDeleteKey(*keyp, vkey);
  205.     RegFlushKey(*keyp);
  206.     RegCloseKey(*keyp);
  207.  
  208.     if (RegCreateKeyEx(HKEY_CURRENT_USER, "\\Software",
  209.         0, 0, REG_OPTION_NON_VOLATILE, KEY_CREATE_SUB_KEY, NULL,
  210.         keyp, &result) != ERROR_SUCCESS)
  211.         return;
  212.  
  213.     for(i=0, cp = rgp->ri_basekey; cp && *cp; cp++)
  214.         if (*cp != '\\')
  215.            vkey[i++] = *cp;
  216.     vkey[i] = 0;
  217.  
  218.     RegDeleteKey(*keyp, vkey);
  219.     RegFlushKey(*keyp);
  220.     RegCloseKey(*keyp);
  221. }
  222.  
  223. BOOL
  224. GetRegistryValues(int version, HWND hwnd, registry_t *setp,
  225.                   reginfo_t *rgp, int nsetp, BOOL installflag)
  226. {
  227.     HKEY hkey;
  228.     int result, i;
  229.     registry_t *stp;
  230.     BOOL status;
  231.     extern void Copyright(int, HWND);
  232.     
  233.   restart:
  234.     hkey = NULL;
  235.     result = 0;
  236.     
  237.     sprintf(vkey,"\\Software\\%s%s%s%s", rgp->ri_basekey, rgp->ri_subkey1,
  238.         rgp->ri_subkey2, rgp->ri_subkey3);
  239.  
  240.     if (RegCreateKeyEx(HKEY_CURRENT_USER, vkey, 0, 0, REG_OPTION_NON_VOLATILE,
  241.         KEY_READ, NULL, &hkey, &result) != ERROR_SUCCESS) {
  242.             usermsg(hwnd, "Unable to open system registry");
  243.             return FALSE;
  244.     }
  245.     if (result != REG_OPENED_EXISTING_KEY && installflag) {
  246.         
  247.         sprintf(tmpbuf,"%s has not been installed for \"%s\". Do you whish to install it ?",
  248.             rgp->ri_msgstring, rgp->ri_username);
  249.  
  250.         if (yesno(hwnd, tmpbuf)) {
  251.             if (!Install(version, hwnd, hkey, setp, rgp, nsetp, FALSE)) {
  252.                 RegCleanup(version, hkey, rgp);
  253.                 sprintf(tmpbuf, "%s HAS NOT BEEN INSTALLED!", rgp->ri_msgstring);
  254.                 usermsg(hwnd, tmpbuf);
  255.                 return FALSE;
  256.             }
  257.             else {
  258.                 sprintf(tmpbuf, "%s is now installed for \"%s\".",
  259.                     rgp->ri_msgstring, rgp->ri_username);
  260.                 usermsg(hwnd, tmpbuf);
  261.                 Copyright(version, hwnd);
  262.                 goto restart;
  263.             }
  264.         }
  265.         else {
  266.             RegCleanup(version, hkey, rgp);
  267.             return FALSE;
  268.         }
  269.     }    
  270.     status = TRUE;
  271.     
  272.     for (i=0, stp = setp; i< nsetp; stp++,i++) {
  273.         stp->st_keyvalue[0] = 0;
  274.         stp->st_keylen = stp->st_maxkeylen;
  275.  
  276.         if (RegQueryValueEx(hkey, stp->st_keyname, 0, &result,
  277.             stp->st_keyvalue, &stp->st_keylen) != ERROR_SUCCESS) {
  278.             if (!installflag) {
  279.                 status = FALSE;
  280.                 break;
  281.             }
  282.             sprintf(tmpbuf, "%s is not fully installed. Do you wish to re-install it ?",
  283.                 rgp->ri_msgstring);
  284.  
  285.             if (yesno(hwnd, tmpbuf)) {
  286.                 if (!Install(version, hwnd, hkey, setp, rgp, nsetp, FALSE)) {
  287.                     RegCleanup(version, hkey, rgp);
  288.                     sprintf(tmpbuf, "%s HAS NOT BEEN FULLY INSTALLED!",
  289.                         rgp->ri_msgstring);                    
  290.                     usermsg(hwnd, tmpbuf);
  291.                     return FALSE;
  292.                 }
  293.                 else {
  294.                     sprintf(tmpbuf, "%s is now installed for \"%s\".",
  295.                         rgp->ri_msgstring, rgp->ri_username);
  296.                     usermsg(hwnd, tmpbuf);
  297.                     goto restart;
  298.                 }
  299.             }
  300.             else
  301.                 return FALSE;
  302.         }
  303.         if (result != REG_BINARY)
  304.             stp->st_keyvalue[stp->st_keylen] = 0;   /* Important */
  305.     }
  306.     if (hkey)
  307.         RegCloseKey(hkey);
  308.  
  309.     return status;
  310. }
  311.  
  312. #else /* WINNT_31 */
  313.  
  314. void
  315. RegCleanup(version, hxkey, rgp)
  316. int version;
  317. HKEY hxkey;
  318. reginfo_t *rgp;
  319. {
  320.     int  result;
  321.     char *cp, vkey[200];
  322.     HKEY *keyp, tmpkey = NULL;
  323.     extern char *StrError(int);
  324.  
  325.     keyp = (hxkey) ? &hxkey : &tmpkey;
  326.     if (hxkey)
  327.         RegCloseKey(hxkey);
  328.  
  329.     sprintf(vkey,"Software\\%s%s%s", rgp->ri_basekey, rgp->ri_subkey1,
  330.         rgp->ri_subkey2, rgp->ri_subkey3);
  331.  
  332.     do {
  333.         if ((result = RegOpenKeyEx(HKEY_CURRENT_USER, vkey, 0,
  334.             KEY_WRITE, keyp)) != ERROR_SUCCESS) {
  335.             sprintf(tmpbuf,"Can't open [ %s ]: %s", vkey, StrError(result));
  336.             usermsg(NULL, tmpbuf);
  337.             return;
  338.         }
  339.         if (strcmp(vkey,"Software")==0)
  340.             break;
  341.         if ((result = RegDeleteKey(*keyp, NULL)) != ERROR_SUCCESS) {
  342.             sprintf(tmpbuf,"Can't delete key [ %s ]: %s", vkey,
  343.                 StrError(result));
  344.             usermsg(NULL, tmpbuf);
  345.         }
  346.         RegFlushKey(*keyp);
  347.         RegCloseKey(*keyp);
  348.         cp=strrchr(vkey,'\\');
  349.  
  350.         if (cp)
  351.             cp = 0;
  352.  
  353.     } while (cp && strcmp(vkey,"Software"));
  354. }
  355.  
  356. BOOL
  357. GetRegistryValues(int version, HWND hwnd, registry_t *setp,
  358.                   reginfo_t *rgp, int nsetp, BOOL installflag)
  359. {
  360.     HKEY hkey;
  361.     int result, i;
  362.     registry_t *stp;
  363.     BOOL status;
  364.     extern void Copyright(int, HWND);
  365.     extern char *StrError(int);
  366.     
  367.   restart:
  368.     hkey = NULL;
  369.     result = 0;
  370.     
  371.     sprintf(vkey,"Software\\%s%s%s%s", rgp->ri_basekey, rgp->ri_subkey1,
  372.         rgp->ri_subkey2, rgp->ri_subkey3);
  373.  
  374.     result = RegOpenKeyEx(HKEY_CURRENT_USER, vkey, 0, KEY_READ, &hkey);
  375.  
  376.     if (result != ERROR_SUCCESS && installflag) {
  377.         
  378.         sprintf(tmpbuf,"%s has not been installed for \"%s\". Do you whish to install it ?",
  379.             rgp->ri_msgstring, rgp->ri_username);
  380.  
  381.         if (yesno(hwnd, tmpbuf)) {
  382.             if (!Install(version, hwnd, hkey, setp, rgp, nsetp, FALSE)) {
  383.                 RegCleanup(version, hkey, rgp);
  384.                 sprintf(tmpbuf, "%s HAS NOT BEEN INSTALLED!", rgp->ri_msgstring);
  385.                 usermsg(hwnd, tmpbuf);
  386.                 return FALSE;
  387.             }
  388.             else {
  389.                 sprintf(tmpbuf, "%s is now installed for \"%s\".",
  390.                     rgp->ri_msgstring, rgp->ri_username);
  391.                 usermsg(hwnd, tmpbuf);
  392.                 Copyright(version, hwnd);
  393.                 goto restart;
  394.             }
  395.         }
  396.         else {
  397.             sprintf(tmpbuf,"Key not found [ %s ]: %s", vkey, StrError(result));
  398.             return FALSE;
  399.         }
  400.     }    
  401.     status = TRUE;
  402.     
  403.     for (i=0, stp = setp; i< nsetp; stp++,i++) {
  404.         stp->st_keyvalue[0] = 0;
  405.         stp->st_keylen = stp->st_maxkeylen;
  406.  
  407.         if (RegQueryValueEx(hkey, stp->st_keyname, 0, &result,
  408.             stp->st_keyvalue, &stp->st_keylen) != ERROR_SUCCESS) {
  409.             if (!installflag) {
  410.                 status = FALSE;
  411.                 break;
  412.             }
  413.             sprintf(tmpbuf, "%s is not fully installed. Do you wish to re-install it ?",
  414.                 rgp->ri_msgstring);
  415.  
  416.             if (yesno(hwnd, tmpbuf)) {
  417.                 if (!Install(version, hwnd, hkey, setp, rgp, nsetp, FALSE)) {
  418.                     RegCleanup(version, hkey, rgp);
  419.                     sprintf(tmpbuf, "%s HAS NOT BEEN FULLY INSTALLED!",
  420.                         rgp->ri_msgstring);                    
  421.                     usermsg(hwnd, tmpbuf);
  422.                     return FALSE;
  423.                 }
  424.                 else {
  425.                     sprintf(tmpbuf, "%s is now installed for \"%s\".",
  426.                         rgp->ri_msgstring, rgp->ri_username);
  427.                     usermsg(hwnd, tmpbuf);
  428.                     Copyright(version, hwnd);
  429.                     goto restart;
  430.                 }
  431.             }
  432.             else
  433.                 return FALSE;
  434.         }
  435.         if (result != REG_BINARY)
  436.             stp->st_keyvalue[stp->st_keylen] = 0;   /* Important */
  437.     }
  438.     if (hkey)
  439.         RegCloseKey(hkey);
  440.  
  441.     return status;
  442. }
  443.  
  444. #endif /* WINNT_31 */
  445.